<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="table.where" label-width="77px" class="ele-form-search"
               @keyup.enter.native="$refs.table.reload()" @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :md="6" :sm="12">
            <el-form-item label="编码内容:">
              <el-input v-model="table.where.zzcode" placeholder="请输入编码内容" clearable/>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <el-form-item label="编码描述:">
              <el-input v-model="table.where.bz" placeholder="请输入编码描述" clearable/>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <div class="ele-form-actions">
              <el-button type="primary" size="small" @click="$refs.table.reload()" icon="el-icon-search" class="ele-btn-icon">查询
              </el-button>
              <el-button  size="small" @click="(table.where={})&&$refs.table.reload()">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 操作按钮 -->
      <div class="ele-table-tool ele-table-tool-default">
        <el-button @click="showEdit=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small" v-if="permission.includes('sys:code_rules:add')">添加
        </el-button>
        <el-button @click="remove()" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small" v-if="permission.includes('sys:code_rules:dall')">批量删除
        </el-button>
      </div>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" v-if="table" :config="table" size="mini" :choose.sync="choose" height="calc(100vh - 265px)" highlight-current-row>
        <template slot-scope="{index}">
          <el-table-column type="selection" width="45" align="center" fixed="left"/>
          <el-table-column type="index" :index="index" label="序号" width="60" align="center" fixed="left" show-overflow-tooltip/>
          <el-table-column prop="zzcode" label="编码内容" sortable="custom" show-overflow-tooltip  min-width="150"/>
          <el-table-column prop="bz" label="编码描述" sortable="custom" show-overflow-tooltip min-width="150"/>
          <el-table-column prop="headvalue" label="编码头" sortable="custom" show-overflow-tooltip min-width="120"/>
          <el-table-column prop="maxvalue" label="最大值" sortable="custom" show-overflow-tooltip min-width="120"/>
          <el-table-column prop="dz" label="递增数量" sortable="custom" show-overflow-tooltip min-width="150"/>
          <el-table-column prop="length" label="编号长度" sortable="custom" show-overflow-tooltip min-width="150">
          </el-table-column>
                <el-table-column
            prop="isyear"
            label="是否用年"
            sortable="custom"
            :resizable="false"
            min-width="120"
          >
            <template slot-scope="{ row }">
              <el-switch
                v-model="row.isyear"
                :active-value=true
                :inactive-value=false
              />
            </template>
          </el-table-column>
          <el-table-column prop="ismonth" label="是否用月" sortable="custom" show-overflow-tooltip min-width="150">
            <template slot-scope="{row}">
              <el-switch v-model="row.ismonth" :active-value=true :inactive-value=false />
            </template>
          </el-table-column>
          <el-table-column prop="isday" label="是否用日" sortable="custom" show-overflow-tooltip min-width="150">
            <template slot-scope="{row}">
              <el-switch v-model="row.isday" :active-value=true :inactive-value=false />
            </template>
          </el-table-column>
          <el-table-column prop="backtype" label="归零条件" sortable="custom" show-overflow-tooltip min-width="150">
            <!--
            <template slot-scope="{row}">
              <el-switch v-model="row.status" @change="editStatus(row)" :active-value="1" :inactive-value="2"/>
            </template>
            -->
          </el-table-column>
          <el-table-column label="操作" width="130px" align="center" :resizable="false"  fixed="right">
            <template slot-scope="{row}">
              <el-link @click="edit(row)" icon="el-icon-edit" type="primary" :underline="false" v-if="permission.includes('sys:code_rules:edit')">修改</el-link>
              <el-popconfirm title="确定要删除此编码规则吗？" @confirm="remove(row)" class="ele-action">
                <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false" v-if="permission.includes('sys:code_rules:delete')">删除</el-link>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <el-dialog :title="editForm.id?'修改编码规则':'新增编码规则'" :visible.sync="showEdit" width="900px"
               @closed="clearData()" :destroy-on-close="false" :lock-scroll="false"  :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="editRules" label-width="102px">
        <el-row :gutter="25" class="el-row">
          <el-col span="8" >
            <el-form-item label="编码内容:" prop="zzcode">
              <el-input v-model="editForm.zzcode" placeholder="请输入编码内容" clearable/>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item label="编码描述:" prop="bz">
              <el-input v-model="editForm.bz" placeholder="请输入编码描述" clearable/>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item label="编码头:" prop="headvalue">
              <el-input v-model="editForm.headvalue" placeholder="请输入编码头" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="25" class="el-row">
          <el-col span="8">
            <el-form-item label="递增步长:">
              <el-input-number v-model="editForm.dz" :step="1" :min="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item label="最大值:">
              <el-input-number v-model="editForm.maxvalue" :step="1" :min="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item label="编号长度:">
              <el-input-number v-model="editForm.length" :min="2" :step="1"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="25" class="el-row">
          <el-col span="8">
            <el-form-item label="归零条件:">
              <el-select v-model="editForm.backtype" placeholder="请选择" class="ele-block" clearable>
                <el-option label="按日" value="d"></el-option>
                <el-option label="按月" value="m"></el-option>
                <el-option label="按年" value="y"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="16" >
            <el-form-item label="">
              <el-checkbox  :checked="editForm.isyear==1 ? 1:0" :key="editForm.isyear"   label="是否用年" v-model="editForm.isyear" ></el-checkbox>
              <el-checkbox  :checked="editForm.ismonth==1 ? 1:0" :key="editForm.ismonth"   label="是否用月" v-model="editForm.ismonth" />
              <el-checkbox  :checked="editForm.isday==1 ? 1:0"  :key="editForm.isday"   label="是否用日" v-model="editForm.isday" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="25" class="el-row" v-if="false">
          <el-col span="24">
            <el-form-item label="">
              <el-checkbox  :checked="editForm.includeProject==1 ? 1:0"  :key="editForm.includeProject" label="包含项目代码" v-model="editForm.includeProject" />
              <el-checkbox  :checked="editForm.includeProduct==1 ?  1:0" :key="editForm.includeProduct" label="包含产品代码" v-model="editForm.includeProduct" />
              <el-checkbox   :checked="editForm.includeCreater==1 ?  1:0" :key="editForm.includeCreater" label="包含创建人代码" v-model="editForm.includeCreater" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="showEdit=false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
export default {
  name: "SysCodeRules",
  data() {
    return {
      table: {url: '/coderules/index', where: {}},  // 表格配置
      choose: [],  // 表格选中数据
      showEdit: false,  // 是否显示表单弹窗
      editForm: {},  // 表单数据
      backtypeList: [],
      editRules: {  // 表单验证规则
        zzcode: [
          {required: true, message: '请输入编码内容', trigger: 'blur'}
        ],
        bz: [
          {required: true, message: '请输入编码描述', trigger: 'blur'}
        ],

      },
    }
  },computed:{
   ...mapGetters(["permission"])
  },
  mounted() {
    //this.getbacktypeList();
  },
  methods: {
    // 关闭
    clearData(){

       this.editForm={};

    },
    /* 显示编辑 */
    edit(row) {
      this.editForm = Object.assign({}, row);
      console.log(this.editForm )
      this.showEdit = true;
    },
    /* 保存编辑 */
    save() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({lock: true});
          this.$http[this.editForm.id ? 'put' : 'post'](this.editForm.id ? '/coderules/edit' : '/coderules/add', this.editForm).then(res => {
            loading.close();
            console.log(res.data)
            if (res.data.code === 0) {
              this.showEdit = false;
              this.$message({type: 'success', message: res.data.msg});
              this.$refs.table.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    getbacktypeList(){
      var codeGroup="coderule@backtype";
      this.$http.get('/codegroupdetail/getCodeGroupDetailList/'+codeGroup).then(res => {
      this.backtypeList = res.data.data;
    }).catch(e => {
      this.$message.error(e.message);
    });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({}, row);
      this.$http.put('/coderules/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.status = !row.status ? 2 : 1;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 删除 */
    remove(row) {
      if (!row) {  // 批量删除
        if (this.choose.length === 0) return this.$message.error('请至少选择一条数据');
        let ids = this.choose.map(d => d.id);
        this.$confirm('确定要删除选中的客户吗?', '提示', {type: 'warning'}).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.delete('/coderules/delete/' + ids).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              this.$refs.table.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => 0);
      } else {  // 单个删除
        const loading = this.$loading({lock: true});
        this.$http.delete('/coderules/delete/' + [row.id]).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({type: 'success', message: res.data.msg});
            this.$refs.table.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }
    },
  }
}
</script>

<style scoped>
.ele-block >>> .el-upload, .ele-block >>> .el-upload-dragger {
  width: 100%;
}
.el-row {
    margin-bottom: 20px;
    display: flex;
    flex-wrap: wrap
  }

.el-input-number {
  width: 168px;
  line-height: 30px;
  height: 28px;
}
</style>
